package com.eedi.framework.oauth2.dal.mapper;

import cn.hutool.core.util.ObjUtil;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.eedi.framework.oauth2.controller.sys.vo.client.SysOAuth2ClientPageReq;
import com.eedi.framework.oauth2.dal.dataobject.SysOAuth2ClientDO;
import org.apache.ibatis.annotations.Mapper;


/**
 * OAuth2 客户端 Mapper
 *
 * @author 永聚长青源码
 */
@Mapper
public interface SysOAuth2ClientMapper extends BaseMapperX<SysOAuth2ClientDO> {

    default PageResult<SysOAuth2ClientDO> selectPage(SysOAuth2ClientPageReq reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SysOAuth2ClientDO>()
                .likeIfPresent(SysOAuth2ClientDO::getName, reqVO.getName())
                .eqIfPresent(SysOAuth2ClientDO::getStatus, reqVO.getStatus())
                .orderByDesc(SysOAuth2ClientDO::getId));
    }

    default SysOAuth2ClientDO selectByClientCode(String clientCode) {
        return selectOne(SysOAuth2ClientDO::getClientCode, clientCode);
    }

    default String getSysClientIdBySysTenantId(String sysTenantId){
        SysOAuth2ClientDO sysOauth2Client = selectOne(SysOAuth2ClientDO::getSysTenantId, sysTenantId);
        if (ObjUtil.isNotNull(sysOauth2Client)){
            return sysOauth2Client.getClientCode();
        }
        return null;
    }

}
